package com.lkm.app_ui.help;

import com.lkm.app_ui.SerialPortManager;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidParameterException;

import android_serialport_api.SerialPort;

public class DevicePrinter {
    private static byte[] Align_Center = new byte[]{(byte) 27, (byte) 97, (byte) 1};
    private static byte[] Print_Init = new byte[]{(byte) 27, (byte) 64};
    private static byte[] entint = new byte[]{(byte) 10};
    private String charsetName;
    private SerialPortManager serialPortManager;

    public DevicePrinter(String serialpath, int serialbaudrate) {
        this(serialpath, serialbaudrate, "GB2312");
    }

    public DevicePrinter(final String serialpath, final int serialbaudrate, String charsetName) {
        this.serialPortManager = null;
        this.charsetName = null;
        this.charsetName = charsetName;
        this.serialPortManager = new SerialPortManager() {
            protected void onDataReceived(byte[] buffer, int size) {
            }

            protected void initPort() {
                try {
                    this.mSerialPort = DevicePrinter.this.getSerialPort(serialpath, serialbaudrate);
                    this.mOutputStream = this.mSerialPort.getOutputStream();
                    this.mInputStream = this.mSerialPort.getInputStream();
                    this.mReadThread = new ReadThread();
                    this.mReadThread.start();
                } catch (Exception e) {
                }
            }
        };
    }

    private SerialPort getSerialPort(String serialpath, int baudrate) throws SecurityException, IOException, InvalidParameterException {
        return new SerialPort(new File(serialpath), baudrate, 0);
    }
    public void write(byte b[]) throws Exception {
        this.serialPortManager.getOutputStream().write(b);
        this.serialPortManager.getOutputStream().flush();
    }
    public void print(String content) {
        for (String s : content.replace("\n", " \n").split("\n")) {
            try {
                if (s.indexOf("[left]") >= 0) {
                    this.serialPortManager.getOutputStream().write(Print_Init);
                } else if (s.indexOf("[center]") >= 0) {
                    this.serialPortManager.getOutputStream().write(Align_Center);
                } else {
                    this.serialPortManager.getOutputStream().write(s.getBytes(this.charsetName));
                    this.serialPortManager.getOutputStream().write(entint);
                }
            } catch (Exception e) {
            }
        }
    }

    public void printQRCode(String strCodedata, int nWidthX, int nVersion, int nErrorCorrectionLevel) {
        try {

            this.serialPortManager.getOutputStream().write(printQR(strCodedata, nWidthX));
        } catch (Exception e) {
        }
    }

    private byte[] printQR(String qr_str, int qr_size) {
        byte[] data1;
        byte[] data3;
        byte[] chushihua = new byte[]{(byte) 27, (byte) 64};
        byte[] qrbyte = null;
        if (qr_size == 2) {
            data1 = new byte[8];
            data1[0] = (byte) 29;
            data1[1] = (byte) 40;
            data1[2] = (byte) 107;
            data1[3] = (byte) 3;
            data1[5] = (byte) 49;
            data1[6] = (byte) 67;
            data1[7] = (byte) 2;
        } else if (qr_size == 4) {
            data1 = new byte[8];
            data1[0] = (byte) 29;
            data1[1] = (byte) 40;
            data1[2] = (byte) 107;
            data1[3] = (byte) 3;
            data1[5] = (byte) 49;
            data1[6] = (byte) 67;
            data1[7] = (byte) 4;
        } else if (qr_size == 5) {
            data1 = new byte[8];
            data1[0] = (byte) 29;
            data1[1] = (byte) 40;
            data1[2] = (byte) 107;
            data1[3] = (byte) 3;
            data1[5] = (byte) 49;
            data1[6] = (byte) 67;
            data1[7] = (byte) 5;
        } else if (qr_size == 6) {
            data1 = new byte[8];
            data1[0] = (byte) 29;
            data1[1] = (byte) 40;
            data1[2] = (byte) 107;
            data1[3] = (byte) 3;
            data1[5] = (byte) 49;
            data1[6] = (byte) 67;
            data1[7] = (byte) 6;
        } else if (qr_size == 7) {
            data1 = new byte[8];
            data1[0] = (byte) 29;
            data1[1] = (byte) 40;
            data1[2] = (byte) 107;
            data1[3] = (byte) 3;
            data1[5] = (byte) 49;
            data1[6] = (byte) 67;
            data1[7] = (byte) 7;
        } else if (qr_size == 8) {
            data1 = new byte[8];
            data1[0] = (byte) 29;
            data1[1] = (byte) 40;
            data1[2] = (byte) 107;
            data1[3] = (byte) 3;
            data1[5] = (byte) 49;
            data1[6] = (byte) 67;
            data1[7] = (byte) 8;
        } else {
            data1 = new byte[8];
            data1[0] = (byte) 29;
            data1[1] = (byte) 40;
            data1[2] = (byte) 107;
            data1[3] = (byte) 3;
            data1[5] = (byte) 49;
            data1[6] = (byte) 67;
            data1[7] = (byte) 3;
        }
        try {
            qrbyte = qr_str.getBytes("GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte[] data2 = new byte[8];
        data2[0] = (byte) 29;
        data2[1] = (byte) 40;
        data2[2] = (byte) 107;
        data2[3] = (byte) 3;
        data2[5] = (byte) 49;
        data2[6] = (byte) 69;
        data2[7] = (byte) 48;
        byte[] data30 = new byte[]{(byte) 29, (byte) 40, (byte) 107};
        if (qrbyte.length > 12) {
            data3 = hexStr2Bytes(toHex(data30, 0, data30.length) + " " + Integer.toHexString(qrbyte.length + 3));
        } else {
            data3 = hexStr2Bytes(toHex(data30, 0, data30.length) + " 0" + Integer.toHexString(qrbyte.length + 3));
        }
        byte[] data31 = new byte[4];
        data31[1] = (byte) 49;
        data31[2] = (byte) 80;
        data31[3] = (byte) 48;
        byte[] data4 = new byte[]{(byte) 27, (byte) 97, (byte) 1};
        byte[] data5 = new byte[8];
        data5[0] = (byte) 29;
        data5[1] = (byte) 40;
        data5[2] = (byte) 107;
        data5[3] = (byte) 3;
        data5[5] = (byte) 49;
        data5[6] = (byte) 82;
        data5[7] = (byte) 48;
        byte[] data6 = new byte[8];
        data6[0] = (byte) 29;
        data6[1] = (byte) 40;
        data6[2] = (byte) 107;
        data6[3] = (byte) 3;
        data6[5] = (byte) 49;
        data6[6] = (byte) 81;
        data6[7] = (byte) 48;
        return  byteArraysToBytes(new byte[][]{chushihua, data1, data2, data3, data31, qrbyte, data4, data5, data6});
    }

    public static byte[] byteArraysToBytes(byte[][] data) {
        int i;
        int length = 0;
        for (byte[] length2 : data) {
            length += length2.length;
        }
        byte[] send = new byte[length];
        int k = 0;
        for (i = 0; i < data.length; i++) {
            int j = 0;
            while (j < data[i].length) {
                int k2 = k + 1;
                send[k] = data[i][j];
                j++;
                k = k2;
            }
        }
        return send;
    }

    public static byte[] hexStr2Bytes(String src) {
        String src2 = "";
        String[] s = src.split(" ");
        for (String append : s) {
            src2 = new StringBuilder(String.valueOf(src2)).append(append).toString();
        }
        int l = src2.length() / 2;
        byte[] ret = new byte[l];
        for (int i = 0; i < l; i++) {
            int m = (i * 2) + 1;
            ret[i] = uniteBytes(src2.substring(i * 2, m), src2.substring(m, m + 1));
        }
        return ret;
    }

    private static byte uniteBytes(String src0, String src1) {
        return (byte) (((byte) (Byte.decode("0x" + src0).byteValue() << 4)) | Byte.decode("0x" + src1).byteValue());
    }

    public static final String toHex(byte[] data, int off, int length) {
        StringBuffer buf = new StringBuffer(data.length * 2);
        for (int i = off; i < length; i++) {
            if ((data[i] & 255) < 16) {
                buf.append("0");
            }
            buf.append(Long.toString((long) (data[i] & 255), 16));
            if (i < data.length - 1) {
                buf.append(" ");
            }
        }
        return buf.toString().toUpperCase();
    }

    public byte[] getQRcodeByt(String strCodedata, int nWidthX, int nVersion, int nErrorCorrectionLevel) {
        int i;
        int i2;
        if (nWidthX < 2) {
            i = 1;
        } else {
            i = 0;
        }
        if (nWidthX > 6) {
            i2 = 1;
        } else {
            i2 = 0;
        }
        i |= i2;
        if (nErrorCorrectionLevel < 1) {
            i2 = 1;
        } else {
            i2 = 0;
        }
        i |= i2;
        if (nErrorCorrectionLevel > 4) {
            i2 = 1;
        } else {
            i2 = 0;
        }
        if ((i2 | i) != 0) {
            return null;
        }
        if (nVersion < 0 || nVersion > 16) {
            return null;
        }
        try {
            byte[] bCodeData = strCodedata.getBytes(this.charsetName);
            ESCCmd.GS_w_n[2] = (byte) nWidthX;
            ESCCmd.GS_k_m_v_r_nL_nH[3] = (byte) nVersion;
            ESCCmd.GS_k_m_v_r_nL_nH[4] = (byte) nErrorCorrectionLevel;
            ESCCmd.GS_k_m_v_r_nL_nH[5] = (byte) (bCodeData.length & 255);
            ESCCmd.GS_k_m_v_r_nL_nH[6] = (byte) ((bCodeData.length & 65280) >> 8);
            return  byteArraysToBytes(new byte[][]{ESCCmd.GS_w_n, ESCCmd.GS_k_m_v_r_nL_nH, bCodeData});
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    public static class ESCCmd {
        public static byte[] CR = new byte[]{(byte) 13};
        public static byte[] DES_ENCRYPT = new byte[]{(byte) 31, (byte) 31, (byte) 1};
        public static byte[] DES_SETKEY;
        public static byte[] DLE_DC4_n_m_t;
        public static byte[] ERROR = new byte[1];
        public static byte[] ESC_3_n;
        public static byte[] ESC_ALT = new byte[]{(byte) 27, (byte) 64};
        public static byte[] ESC_CAN = new byte[]{(byte) 24};
        public static byte[] ESC_FF = new byte[]{(byte) 27, (byte) 12};
        public static byte[] ESC_L = new byte[]{(byte) 27, (byte) 76};
        public static byte[] ESC_M_n;
        public static byte[] ESC_R_n;
        public static byte[] ESC_S = new byte[]{(byte) 27, (byte) 83};
        public static byte[] ESC_SP_n;
        public static byte[] ESC_T_n;
        public static byte[] ESC_V_n;
        public static byte[] ESC_W_xL_xH_yL_yH_dxL_dxH_dyL_dyH;
        public static byte[] ESC_a_n;
        public static byte[] ESC_dollors_nL_nH;
        public static byte[] ESC_lbracket_n;
        public static byte[] ESC_line_n;
        public static byte[] ESC_t_n;
        public static byte[] FF = new byte[]{(byte) 12};
        public static byte[] FS_line_n;
        public static byte[] FS_p_n_m;
        public static byte[] GS_B_n;
        public static byte[] GS_E_n;
        public static byte[] GS_H_n;
        public static byte[] GS_P_x_y;
        public static byte[] GS_V_m;
        public static byte[] GS_V_m_n;
        public static byte[] GS_W_nL_nH = new byte[]{(byte) 29, (byte) 87, (byte) 118, (byte) 2};
        public static byte[] GS_backslash_m;
        public static byte[] GS_backslash_nL_nH;
        public static byte[] GS_dollors_nL_nH;
        public static byte[] GS_exclamationmark_n;
        public static byte[] GS_f_n;
        public static byte[] GS_h_n = new byte[]{(byte) 29, (byte) 104, (byte) -94};
        public static byte[] GS_k_m_n_ = new byte[]{(byte) 29, (byte) 107, (byte) 65, (byte) 12};
        public static byte[] GS_k_m_v_r_nL_nH;
        public static byte[] GS_leftbracket_k_pL_pH_cn_67_n;
        public static byte[] GS_leftbracket_k_pL_pH_cn_69_n;
        public static byte[] GS_leftbracket_k_pL_pH_cn_80_m__d1dk;
        public static byte[] GS_leftbracket_k_pL_pH_cn_fn_m;
        public static byte[] GS_w_n = new byte[]{(byte) 29, (byte) 119, (byte) 3};
        public static byte[] LF = new byte[]{(byte) 10};

        static {
            byte[] bArr = new byte[13];
            bArr[0] = (byte) 31;
            bArr[1] = (byte) 31;
            bArr[3] = (byte) 8;
            bArr[5] = (byte) 1;
            bArr[6] = (byte) 1;
            bArr[7] = (byte) 1;
            bArr[8] = (byte) 1;
            bArr[9] = (byte) 1;
            bArr[10] = (byte) 1;
            bArr[11] = (byte) 1;
            bArr[12] = (byte) 1;
            DES_SETKEY = bArr;
            bArr = new byte[4];
            bArr[0] = (byte) 29;
            bArr[1] = (byte) 80;
            GS_P_x_y = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 27;
            bArr[1] = (byte) 82;
            ESC_R_n = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 27;
            bArr[1] = (byte) 116;
            ESC_t_n = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 27;
            bArr[1] = (byte) 51;
            ESC_3_n = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 27;
            bArr[1] = (byte) 32;
            ESC_SP_n = bArr;
            bArr = new byte[5];
            bArr[0] = (byte) 16;
            bArr[1] = (byte) 20;
            bArr[2] = (byte) 1;
            bArr[4] = (byte) 1;
            DLE_DC4_n_m_t = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 29;
            bArr[1] = (byte) 86;
            GS_V_m = bArr;
            bArr = new byte[4];
            bArr[0] = (byte) 29;
            bArr[1] = (byte) 86;
            bArr[2] = (byte) 66;
            GS_V_m_n = bArr;
            bArr = new byte[4];
            bArr[0] = (byte) 27;
            bArr[1] = (byte) 36;
            ESC_dollors_nL_nH = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 27;
            bArr[1] = (byte) 97;
            ESC_a_n = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 29;
            bArr[1] = (byte) 33;
            GS_exclamationmark_n = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 27;
            bArr[1] = (byte) 77;
            ESC_M_n = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 27;
            bArr[1] = (byte) 69;
            GS_E_n = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 27;
            bArr[1] = (byte) 45;
            ESC_line_n = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 27;
            bArr[1] = (byte) 123;
            ESC_lbracket_n = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 29;
            bArr[1] = (byte) 66;
            GS_B_n = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 27;
            bArr[1] = (byte) 86;
            ESC_V_n = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 29;
            bArr[1] = (byte) 47;
            GS_backslash_m = bArr;
            bArr = new byte[4];
            bArr[0] = (byte) 28;
            bArr[1] = (byte) 112;
            bArr[2] = (byte) 1;
            FS_p_n_m = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 29;
            bArr[1] = (byte) 72;
            GS_H_n = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 29;
            bArr[1] = (byte) 102;
            GS_f_n = bArr;
            bArr = new byte[7];
            GS_k_m_v_r_nL_nH = bArr;
            bArr = new byte[10];
            ESC_W_xL_xH_yL_yH_dxL_dxH_dyL_dyH = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 27;
            bArr[1] = (byte) 84;
            ESC_T_n = bArr;
            bArr = new byte[4];
            bArr[0] = (byte) 29;
            bArr[1] = (byte) 36;
            GS_dollors_nL_nH = bArr;
            bArr = new byte[4];
            bArr[0] = (byte) 29;
            bArr[1] = (byte) 92;
            GS_backslash_nL_nH = bArr;
            bArr = new byte[3];
            bArr[0] = (byte) 28;
            bArr[1] = (byte) 45;
            FS_line_n = bArr;
            bArr = new byte[8];
            bArr[0] = (byte) 29;
            bArr[1] = (byte) 40;
            bArr[2] = (byte) 107;
            bArr[3] = (byte) 3;
            bArr[5] = (byte) 49;
            bArr[6] = (byte) 67;
            bArr[7] = (byte) 3;
            GS_leftbracket_k_pL_pH_cn_67_n = bArr;
            bArr = new byte[8];
            bArr[0] = (byte) 29;
            bArr[1] = (byte) 40;
            bArr[2] = (byte) 107;
            bArr[3] = (byte) 3;
            bArr[5] = (byte) 49;
            bArr[6] = (byte) 69;
            bArr[7] = (byte) 48;
            GS_leftbracket_k_pL_pH_cn_69_n = bArr;
            bArr = new byte[8];
            bArr[0] = (byte) 29;
            bArr[1] = (byte) 40;
            bArr[2] = (byte) 107;
            bArr[3] = (byte) 3;
            bArr[5] = (byte) 49;
            bArr[6] = (byte) 80;
            bArr[7] = (byte) 48;
            GS_leftbracket_k_pL_pH_cn_80_m__d1dk = bArr;
            bArr = new byte[8];
            bArr[0] = (byte) 29;
            bArr[1] = (byte) 40;
            bArr[2] = (byte) 107;
            bArr[3] = (byte) 3;
            bArr[5] = (byte) 49;
            bArr[6] = (byte) 81;
            bArr[7] = (byte) 48;
            GS_leftbracket_k_pL_pH_cn_fn_m = bArr;
        }
    }
}